#import "OmUserPageVC.h"
#import "MJExtension.h"
#import "PrivatePhotosViewController.h"
#import "CustomChatUIViewController.h"
#import "UserApiGetUserPageInfoModel.h"
#import "OmUserPageVCPhotoButton.h"
#import "OmSendGiftView.h"
#import "OmMomentsVC.h"
#import "OmPersonalSettingVC.h"
@interface OmUserPageVC ()
@property (nonatomic, strong) UserApiGetUserPageInfoModel *model;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *headBackgroundCircle;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *onlineLabel;
@property (nonatomic, strong) UILabel *sexLabel;
@property (nonatomic, strong) UIButton *chatButton;
@property (nonatomic, strong) UIButton *backButton;
@property (nonatomic, strong) UIButton *followButton;
@property (nonatomic, strong) OmUserPageVCPhotoButton *photoButton;
@property (nonatomic, strong) UIImageView *levelImgView;
@property (nonatomic, strong) UILabel *naviTitleLbl;
@property (nonatomic, strong) UIButton *moreMessageBtn;
@property (nonatomic, strong) UIButton *sendVipBtn;
@property (nonatomic, strong) UIButton *sendGiftBtn;
@property (nonatomic, strong) UILabel *messageLbl;
@property (nonatomic, strong) UIView *thingsView;
@property (nonatomic, strong) OmSendGiftView *sendGiftView;
@end
@implementation OmUserPageVC
#pragma mark - ConstructionScript
- (void)configSelf {
    self.view.backgroundColor = HexadecimalColor(@"#F7F7F7");
    [self setHiddenForTabBar:YES navigationBar:YES];
}
- (void)allocateComponents {
    self.backgroundImageView = [[UIImageView alloc] initWithImage:[OmHandyMethods imageFromColor:HexadecimalColor(@"#FC7CB5") gradientToColor:HexadecimalColor(@"#FCA7A1") frame:CGRectMake(0, 0, WINDOW_WIDTH, RPX(295))]];
    [self.view addSubview:self.backgroundImageView];
    self.headBackgroundCircle = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"circle")];
    [self.view addSubview:self.headBackgroundCircle];
    self.headImage = [[UIImageView alloc] init].fp_contentMode(UIViewContentModeScaleAspectFill);
    self.headImage.layer.cornerRadius = RPX(37.5);
    self.headImage.clipsToBounds = YES;
    [self.view addSubview:self.headImage];
    self.nameLabel = [[UILabel alloc] init].fp_font(BoldRPXFont(23)).fp_textColor(HexadecimalColor(@"#FFFFFF"));
    [self.view addSubview:self.nameLabel];
    self.onlineLabel = [[UILabel alloc] init].fp_font(BoldRPXFont(13)).fp_textColor(HexadecimalColor(@"#FFFFFF"));
    [self.view addSubview:self.onlineLabel];
    self.sexLabel = [[UILabel alloc] init].fp_font(BoldRPXFont(13)).fp_textColor(HexadecimalColor(@"#FFFFFF"));
    [self.view addSubview:self.sexLabel];
    self.levelImgView = [[UIImageView alloc] init];
    [self.view addSubview:self.levelImgView];
    self.moreMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreMessageBtn setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]];
    [self.moreMessageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.moreMessageBtn.layer.cornerRadius = 17.5;
    [self.moreMessageBtn setTitle:ASLocalizedString(@"更多资料") forState:UIControlStateNormal];
    self.moreMessageBtn.titleLabel.font = [UIFont systemFontOfSize:RPX(13) weight:UIFontWeightMedium];
    [self.moreMessageBtn addTarget:self action:@selector(moreMessageTap) forControlEvents:UIControlEventTouchUpInside];
    self.moreMessageBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
    [self.view addSubview:self.moreMessageBtn];
    self.sendVipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendVipBtn setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:0.2]];
    self.sendVipBtn.layer.cornerRadius = 17.5;
    self.sendVipBtn.titleLabel.font = [UIFont systemFontOfSize:RPX(13) weight:UIFontWeightMedium];
    [self.sendVipBtn setTitle:ASLocalizedString(@"赠送VIP") forState:UIControlStateNormal];
    [self.sendVipBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.sendVipBtn.hidden = YES;
    [self.view addSubview:self.sendVipBtn];
    self.sendGiftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendGiftBtn setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:1 alpha:1.0]];
    self.sendGiftBtn.layer.cornerRadius = 3;
    [self.sendGiftBtn setTitle:ASLocalizedString(@"送礼") forState:UIControlStateNormal];
    [self.sendGiftBtn addTarget:self action:@selector(sendGiftTap) forControlEvents:UIControlEventTouchUpInside];
    self.sendGiftBtn.titleLabel.font = [UIFont systemFontOfSize:RPX(13) weight:UIFontWeightMedium];
    [self.sendGiftBtn setTitleColor:HexadecimalColor(@"#FA5A8C") forState:UIControlStateNormal];
    if ([self.ID isEqualToString:curUser.id]) self.sendGiftBtn.hidden = YES;
    [self.view addSubview:self.sendGiftBtn];
    self.messageLbl = [[UILabel alloc]init].fp_font([UIFont systemFontOfSize:RPX(16) weight:UIFontWeightHeavy]).fp_textColor([UIColor whiteColor]);
    [self.view addSubview:self.messageLbl];
    self.chatButton = (UIButton *)[[UIButton alloc] init]
    .fp_addTargetActionForControlEvents(self, @selector(onChatButtonTap), UIControlEventTouchUpInside)
    .fp_backgroundColor(HexadecimalColor(@"#FA5A8C"))
    .fp_cornerRadius(RPX(22));
    [self.chatButton setTitle:ASLocalizedString(@"与TA聊天") forState:UIControlStateNormal];
    self.chatButton.titleLabel.font = RPXFont(13);
    [self.chatButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.chatButton];
    self.backButton = [[OmBaseButton alloc] initWithImageName:@"OmBack" mode:UIViewContentModeScaleToFill].fp_addTargetActionForControlEvents(self, @selector(onBackButtonTap), UIControlEventTouchUpInside);
    [self.view addSubview:self.backButton];
    self.followButton = [[UIButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onFollowButtonTap), UIControlEventTouchUpInside);
    [self.followButton.titleLabel setFont:[UIFont systemFontOfSize:RPX(16) weight:UIFontWeightMedium]];
    [self.view addSubview:self.followButton];
    self.naviTitleLbl = [[UILabel alloc]init].fp_font(BoldRPXFont(17)).fp_textColor([UIColor whiteColor]);
    [self.view addSubview:self.naviTitleLbl];
    self.photoButton = [[OmUserPageVCPhotoButton alloc] init].fp_addTargetActionForControlEvents(self, @selector(onPhotoButtonTap), UIControlEventTouchUpInside);
    [self.view addSubview:self.photoButton];
    self.thingsView = [[UIView alloc]init];
    UILabel *titleLbl = [[UILabel alloc]init].fp_font([UIFont systemFontOfSize:RPX(15)]).fp_textColor([UIColor blackColor]);
    titleLbl.text = ASLocalizedString(@"新鲜事");
    [self.thingsView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.thingsView).with.offset(RPX(15));
        make.centerY.equalTo(self.thingsView);
    }];
    UIImageView *arrowImg = [[UIImageView alloc]initWithImage:IMAGE_NAMED(@"OmRightIndex")];
    [self.thingsView addSubview:arrowImg];
    [arrowImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.thingsView).with.offset(- RPX(16));
        make.centerY.equalTo(self.thingsView);
        make.size.mas_equalTo(CGSizeMake(RPX(7), RPX(13)));
    }];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(thingsButtonTap)];
    [self.thingsView addGestureRecognizer:tap];
    self.thingsView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.thingsView];
    self.sendGiftView = [[OmSendGiftView alloc] initWithToUid:self.ID];
    [self.view addSubview:self.sendGiftView];
    self.sendGiftView.hidden = YES;
 }
- (void)layoutComponents {
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@(RPX(295)));
    }];
    [self.headBackgroundCircle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(RPX(85)));
        make.left.equalTo(self.view).offset(RPX(15));
        make.top.equalTo(self.view).offset(RPX(85));
    }];
    [self.headImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(RPX(72)));
        make.center.equalTo(self.headBackgroundCircle);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(RPX(98));
        make.left.equalTo(self.view).offset(RPX(126));
    }];
    [self.onlineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(RPX(10));
    }];
    [self.sexLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.onlineLabel);
        make.left.equalTo(self.onlineLabel.mas_right).offset(RPX(10));
    }];
    [self.levelImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.sexLabel.mas_right).offset(RPX(10));
        make.centerY.equalTo(self.sexLabel);
        make.size.mas_equalTo(CGSizeMake(RPX(15), RPX(15)));
    }];
    [self.moreMessageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(RPX(16));
        make.top.equalTo(self.headBackgroundCircle.mas_bottom).with.offset(RPX(30));
        make.height.equalTo(@(RPX(35)));
    }];
    [self.sendVipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(self.moreMessageBtn);
        make.left.equalTo(self.moreMessageBtn.mas_right).with.offset(RPX(14));
    }];
    [self.sendGiftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).with.offset(- RPX(15));
        make.bottom.equalTo(self.backgroundImageView).with.offset(- RPX(17));
        make.size.mas_equalTo(CGSizeMake(RPX(65), RPX(30)));
    }];
    [self.messageLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(RPX(15));
        make.right.equalTo(self.sendGiftBtn.mas_left).with.offset(10);
        make.centerY.equalTo(self.sendGiftBtn);
    }];
    [self.chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(150)));
        make.height.equalTo(@(RPX(44)));
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(RPX(-92));
    }];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(RPX(9)));
        make.height.equalTo(@(RPX(18)));
        make.top.equalTo(self.view).offset(RPX(33));
        make.left.equalTo(self.view).offset(RPX(15));
    }];
    [self.followButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(RPX(-15));
        make.centerY.equalTo(self.backButton);
    }];
    [self.naviTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backButton);
        make.centerX.equalTo(self.view);
    }];
    [self.photoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.backgroundImageView.mas_bottom).offset(RPX(10));
    }];
    [self.thingsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.photoButton.mas_bottom).with.offset(1);
        make.height.mas_equalTo(RPX(55));
    }];
    [self.sendGiftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(70);
        make.left.right.bottom.equalTo(self.view);
    }];
}
- (void)refreshSelf {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"user_api" A:@"get_user_page_info"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"to_user_id"] = self.ID;
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        self.model = [UserApiGetUserPageInfoModel mj_objectWithKeyValues:responseObject];
        self.photoButton.models = self.model.pictures;
        SaveObjToUserDefault(self.model.avatar, OmChatWithUserAvatarKey);
        SaveObjToUserDefault(self.model.user_nickname, OmChatWithUserNickKey);
    } failure:^(NSString *error) {
        if(StrValid(error))
            [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
#pragma mark - Event
- (void)setModel:(UserApiGetUserPageInfoModel *)model {
    _model = model;
    if (model.blue) {
        self.levelImgView.image = IMAGE_NAMED(@"OmBlueDiamond");
    }else if (model.crown) {
        self.levelImgView.image = IMAGE_NAMED(@"OmKingHat");
    }else {
        self.levelImgView.image = nil;
    }
    self.sexLabel.text = NSStringFormat(@"%@  %ld%@",ASLocalizedString(model.sex == 1 ? @"男" : @"女"),model.age,ASLocalizedString(@"岁"));
    self.naviTitleLbl.text = model.user_nickname;
    self.nameLabel.text = [NSString stringWithFormat:@"%@", model.user_nickname];
    self.onlineLabel.text = ASLocalizedString(model.is_online?@"在线":@"离线");
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    if ([self.ID isEqualToString:curUser.id]) {
        self.followButton.hidden = YES;
    } else {
        [self.followButton setTitle:ASLocalizedString(model.attention ? @"取消关注" : @"关注") forState:UIControlStateNormal];
    }
    self.messageLbl.text = NSStringFormat(@"%ld %@   %ld %@   %ld %@",model.like_count,ASLocalizedString(@"喜欢"),model.attention_fans,ASLocalizedString(@"关注"),model.gift_count,ASLocalizedString(@"礼物"));
}
- (void)onChatButtonTap {
    IMAUser *user = [[IMAUser alloc] initWith:self.model.ID];
    CustomChatUIViewController *vc = [[CustomChatUIViewController alloc] initWith:user];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onBackButtonTap {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)onFollowButtonTap {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"personal_api" A:@"click_attention"];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setObject:self.ID forKey:@"id"];
    [CYNET POST:url parameters:param responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        [MBProgressHUD showSuccessMessage:ASLocalizedString(self.model.attention ? @"取消成功" : @"关注成功")];
        [self refreshSelf];
    } failure:^(NSString *error) {
        [MBProgressHUD showTopTipMessage:error];
    } hasCache:NO];
}
- (void)onPhotoButtonTap {
    PrivatePhotosViewController *vc = [[PrivatePhotosViewController alloc] init];
    vc.touid = self.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)moreMessageTap {
    OmPersonalSettingVC *vc = [[OmPersonalSettingVC alloc] init];
    vc.ID = self.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)sendVipTap {
}
- (void)sendGiftTap {
    self.sendGiftView.hidden = NO;
}
- (void)thingsButtonTap {
    OmMomentsVC *vc = [[OmMomentsVC alloc] init];
    vc.toUserID = self.ID;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
